<!DOCTYPE html>
<title>Verifies that a MediaSource decode error followed by a gc() and page reload does not trigger a crash.</title>
<script src="/w3c/resources/testharness.js"></script>
<script src="/w3c/resources/testharnessreport.js"></script>
<video></video>
<script>
async_test(function(t) {
    var video = document.querySelector('video');
    var mediaSource = new MediaSource();

    mediaSource.onsourceopen = t.step_func(function() {
        mediaSource.onsourceopen = null;
        URL.revokeObjectURL(video.src);

        // Create a SourceBuffer and append garbage so a decode error will occur
        // and the MediaSource will get closed.
        mediaSource.onsourceclose = t.step_func(function() {
            mediaSource.onsourceclose = null;

            // Schedule a GC and page reload. We need a timeout here so that
            // the MediaSource reference used by this event is cleared before
            // we try to GC & reload.
            setTimeout(t.step_func(function() {
                gc();

                var suffix = "?2";
                if (document.location.href.indexOf(suffix) != -1) {
                  t.done();
                  return;
                }

                document.location.href += suffix;
            }), 0);
        });
        var sourceBuffer = mediaSource.addSourceBuffer("video/webm;codecs=\"vp8\"");
        var buffer = new Uint8Array(10);
        sourceBuffer.appendBuffer(buffer);
    });

    video.src = URL.createObjectURL(mediaSource);
});
</script>